<?php
namespace  app\manage\model;
use think\Db;
use think\Model;
class Code extends Model{
    protected $table = 'code';
    /**
     * @param $data
     * @return int|string
     */
    public function m_insert($data) {
        $data = Db::name($this->table)
            ->insert($data);
        return Db::name($this->table)->getLastInsID();
    }

    /**
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_update($code_id, $data) {
        $data = Db::name($this->table)
            ->where(['code_id'=>$code_id])
            ->update($data);
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_delete($code_id) {
        $data = Db::name($this->table)
            ->where(['code_id'=>$code_id])
            ->delete();
        return $data;
    }

    /**
     * @param $code_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($code_id) {
        $data = Db::name($this->table)
            ->where(['code_id'=>$code_id])
            ->field('code_id, admin_tel, code, is_use, create_time')
            ->find();
        return $data;
    }

    /**
     * @param $code_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detailtel($tel) {
        $data = Db::name($this->table)
            ->where(['admin_tel'=>$tel])
            ->field('code_id, admin_tel, code, is_use, create_time')
            ->order('create_time desc')
            ->select();
        if (empty($data)) {
            return '';
        }
        return $data[0];
    }
}